// pages/person/cash/cash.js
const app = getApp()

import Tips from '../../../class/methods/Tips.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: '',
    tixianmoney: '',
    txmoney: '', //输入提现金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    let balance = options.txnum
    that.setData({
      balance: balance
    })
  },

  // inputMoney: function(e) {
  //   var tixianmoney = e.detail.value;
  //   console.log('提现金额', tixianmoney)
  //   let that = this
  //   let money = Number(that.data.money)
  //   console.log(parseInt(money))
  //   if (tixianmoney > money) {
  //     // Tips.alert('')
  //     wx.showModal({
  //       title: '提示',
  //       content: '提现金额大于总金额',
  //     })
  //   } else {
  //     that.setData({
  //       tixianmoney: tixianmoney
  //     })
  //     console.log(tixianmoney)
  //   }

  // },


  allInto: function() {
    let balance = this.data.balance
    this.setData({
      txmoney: balance
    })
    console.log(this.data.txmoney)
  },

  tosubmit(e) {
    let that = this
    // let val = e.detail.value
    let money = e.detail.value.tmoney;
    // let tixianmoney = that.data.tixianmoney
    // console.log(money)

    wx.getStorage({
      key: 'userinfo',
      success: function(res) {
        console.log(res)
        that.setData({
          uid: res.data.id,
        })
        // console.log('val', val)
      },
    })

    if (money <= Number(that.data.balance) && money != 0) {
      // if (tixianmoney > 0) {
      let that = this
      wx.getStorage({
        key: 'userinfo',
        success: function(res) {
          console.log(res, res.data.id)

          console.log(res.data.id)
          that.setData({
            uid: res.data.id,
          })
          Tips.loading()
          wx.request({
            // http://bzl.isoft.mobi/index.php/Home/pay/tixian
            url: app.globalData.apiUrl + "pay/tixian",
            data: {
              uid: that.data.uid,
              money: money
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            success: function(res) {
              Tips.loaded()
              console.log(res)
              if (res.data.code == 0) {
                wx.showToast({
                    title: '提现成功成功',
                    icon: 'success',
                    duration: 2000
                  }),
                  setTimeout(() => {
                    wx.navigateTo({
                      url: '/pages/person/wallet/wallet',
                    })
                  }, 1000);
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })

                // setTimeout(() => {
                //   wx.navigateTo({
                //     url: '/pages/person/wallet/wallet',
                //   })
                // }, 1000);
              }
            },
          })
        },
      })

    } else {
      wx.showModal({
        title: '提示',
        content: '金额不足，无法提现',
      })
    }

  },


  // 全部提现

  // tixian: function() {

  // },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})